<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/4
 * Time: 1:22 AM
 *
 */

namespace common\models;

use common\models\JobType;
use phpDocumentor\Reflection\Types\Integer;

/**
 * Class PartTimeJob
 * @package common\models
 *
 * @property int    $job_type 兼职类型(fk)
 * @property string $color 文字颜色
 * @property string $latitude 纬度
 * @property string $longitude 经度
 * @property string $ad_code 行政区划码
 * @property int    $gender_needed 性别要求
 * @property int    $sign_end 报名结束时间
 * @property int    $start_at 兼职开始时间
 * @property int    $end_at 兼职结束时间
 * @property string $work_start 上班时间
 * @property string $work_end 下班时间
 * @property int    $salary 工资
 * @property int    $salary_type 工资类型
 * @property int    $pay_type 结算方式
 * @property int    $good_id 商品id
 * @property int    $people_num 兼职人数
 *
 */
class PartTimeJob extends Job
{
    public $job_type;
    public $color;
    public $latitude;
    public $longitude;
    public $ad_code;
    public $gender_needed;
    public $sign_end;
    public $start_at;
    public $end_at;
    public $work_start;
    public $work_end;
    public $salary;
    public $salary_type;
    public $pay_type;
    public $good_id;
    public $people_num;
    public $username;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'type'], 'required'],
            [
                [
                    'uid',
                    'type',
                    'status',
                    'forbidden',
                    'pay_needed',
                    'created_at',
                    'updated_at',
                    'job_type',
                    'gender_needed',
                    'sign_end',
                    'salary',
                    'salary_type',
                    'pay_type',
                    'people_num',
                    'hide',
                ],
                'integer'
            ],
            [['content'], 'string'],
            [
                [
                    'title',
                    'contact_name',
                    'contact_phone',
                    'color',
                    'latitude',
                    'longitude',
                    'ad_code',
                ],
                'string',
                'max' => 255
            ],
            [
                ['uid'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => User::className(),
                'targetAttribute' => ['uid' => 'id']
            ],
            [['work_start', 'work_end'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'            => 'ID',
            'uid'           => '发布者',
            'type'          => '类型',
            'status'        => '状态',
            'forbidden'     => '是否屏蔽',
            'pay_needed'    => '需要支付',
            'title'         => '招聘名称',
            'created_at'    => '创建时间',
            'updated_at'    => '最后更新时间',
            'content'       => '兼职详情',
            'contact_name'  => '联系人姓名',
            'contact_phone' => '联系人电话',
            'job_type'      => '兼职类型',
            'color'         => '文字颜色',
            'ad_code'       => '行政区划码',
            'gender_needed' => '性别要求',
            'sign_end'      => '报名结束时间',
            'work_start'    => '上班时间',
            'work_end'      => '下班时间',
            'salary'        => '工资',
            'salary_type'   => '工资类型',
            'pay_type'      => '结算方式',
            'start_at'      => '兼职开始时间',
            'end_at'        => '兼职结束时间',
            'latitude'      => '经度',
            'longitude'     => '纬度',

        ];
    }

//
//    public function beforeSave($insert)
//    {
//        $this->type = Job::PARTTIME_TYPE;
//        if ($insert) {
//            $this->created_at = $this->updated_at = time();
//        } else {
//            $this->created_at = time();
//        }
//
//        return parent::beforeSave($insert);
//    }
//
//    public function afterSave($insert, $changedAttributes)
//    {
//        $info = new JobInfo();
////        var_dump($info);
//        if ($insert) {
//            $info->jid = $this->id;
//        } else {
//            $info = JobInfo::findOne(['jid' => $this->id]);
//            if (empty($info)) {
//                $info = new JobInfo();
//                $info->jid = $this->id;
//            }
//        }
//
//        $info->job_type = empty($this->job_type) ? 1 : $this->job_type;
//        if (isset($this->color)) {
//            $info->color = $this->color;
//        }
//        $info->latitude      = $this->latitude;
//        $info->longitude     = $this->longitude;
//        $info->ad_code       = $this->ad_code;
//        $info->gender_needed = $this->gender_needed;
//        $info->sign_end      = $this->sign_end;
//        $info->start_at      = $this->start_at;
//        $info->end_at        = $this->end_at;
//        $info->work_end      = $this->work_end;
//        $info->work_start    = $this->work_start;
//        $info->salary        = $this->salary;
//        if (isset($this->salary_type)) {
//            $info->salary_type = $this->salary_type;
//        }
//        $info->pay_typed = $this->pay_type;
//        $info->good_id   = $this->good_id;
//        $info->save(false);
//        parent::afterSave($insert, $changedAttributes);
//    }
//
//
    public function afterFind()
    {
        $info                = $this->jobInfo;
        $this->job_type      = $info->job_type;
        $this->color         = $info->color;
        $this->latitude      = $info->latitude;
        $this->longitude     = $info->longitude;
        $this->ad_code       = $info->ad_code;
        $this->gender_needed = $info->gender_needed;
        $this->sign_end      = date('Y-m-d', $info->sign_end);
        $this->start_at      = date('Y-m-d', $info->start_at);
        $this->end_at        = date('Y-m-d', $info->end_at);
        $this->work_end      = $info->work_end;
        $this->work_start    = $info->work_start;
        $this->salary        = $info->salary;
        $this->salary_type = ! empty($info->salary_type) ? $info->salary_type : 1;
        $this->pay_type = ! empty($info->pay_typed) ? $info->pay_typed : 1;
        $this->good_id       = $info->good_id;
        $this->people_num = $info->people_num;

        parent::afterFind();
    }

    /**
     * @return array
     */
    public static function getJonTypes()
    {
        $res   = [];
        $types = JobType::find()->where(['type' => JobType::TYPE_JOB])->all();
        foreach ($types as $type) {
            $res[$type->id] = $type->name;
        }

        return $res;
    }

    public static function getSignedUsersByJid($id = null)
    {
        if (isset($id)) {
            $users = User::find()
                         ->select(['user.*', 'job_record.jid'])
                         ->joinWith('records')
                         ->where([
                             'job_record.jid' => $id,
                             'user.status'    => User::STATUS_ACTIVE,
                         ])->all();
        } else {
            $users = null;
        }

        return $users;
    }


    public static function postPartTime($jid)
    {
//        \Yii::warning("jid:$jid is null");
        $partTime = PartTimeJob::findOne($jid);
//        \Yii::warning($partTime);
        if ($partTime != null) {
            $partTime->forbidden = PartTimeJob::ACCESS;
            $partTime->status    = PartTimeJob::PAY_STATUS;
            $partTime->save(false);
//            \Yii::warning($partTime->errors);
        }
//        \Yii::warning($partTime->status);
    }
}